#Region "Diclaimer"
'Copyright (c) 2006, Mark Bracewell (bracewell at wgg dot com)
'All rights reserved.
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are met: 
'*

'* Redistributions of source code must retain the above copyright
'notice, this list of conditions and the following disclaimer.
'* Redistributions in binary form must reproduce the above copyright
'notice, this list of conditions and the following disclaimer in the
'documentation and/or other materials provided with the distribution.
'* The name Mark Bracewell may not be used to endorse or promote products
'derived from this software without specific prior written permission. 
'* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
'WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'DISCLAIMED. IN NO EVENT SHALL MARK BRACEWELL BE LIABLE FOR ANY
'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
'ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#End Region
Imports SubSonic
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.Collections.Specialized

Namespace Data
    Public Class Member
        Private _joined As Date
        Private _online As Boolean
        Private _email As String
        Private _loginname As String
        Private _lastlogindate
        Private _displayname As String = ""
        Private _posts As Integer = 0
        Private _address As String = ""
        Private _phone As String = ""
        Private _lastpostdate As Date
        Private _signature As String
        Private _bio As String
        Private _sortdesc As Boolean
        Private _msgsperpage As Integer
        Private _showavatars As Boolean
        Private _showsignatures As Boolean
        Private _sendwatchemails As Boolean
        Private _showemail As Boolean
        Private _memberid As Object

        Sub New()
        End Sub

        Sub New(ByVal memberid As Object)
            Load(memberid)
        End Sub

        Sub Load(ByVal member_id As Object)
            _memberid = New Guid(member_id.ToString())
            Dim memUser As Web.Security.MembershipUser = Web.Security.Membership.GetUser(_memberid)
            _joined = memUser.CreationDate
            _online = memUser.IsOnline
            _email = memUser.Email
            _loginname = memUser.UserName
            _lastlogindate = memUser.LastLoginDate
            Dim MemGuid As Guid = memUser.ProviderUserKey()
            Dim MemInfo As New MemberInfo(MemberInfo.Columns.Memberid, MemGuid)
            Dim MemProf As New MemberProfile(MemberProfile.Columns.Memberid, MemGuid)
            _displayname = FirstAndLastName(MemGuid)
            _address = MemInfo.Address
            _phone = MemInfo.Phone
            _signature = MemProf.Signature
            _bio = MemProf.Bio
            _sortdesc = MemProf.Sortdesc
            _msgsperpage = MemProf.Msgsperpage
            _showavatars = MemProf.Showavatars
            _showsignatures = MemProf.Showsignatures
            _sendwatchemails = MemProf.Sendwatchemails
            _showemail = MemProf.Showemail

            Dim qry As New Query(Tables.Messages)
            qry.AddWhere(Messages.Columns.Memberid, MemGuid)
            Dim numposts As Integer = qry.GetRecordCount()
            _posts = numposts.ToString()

            If numposts > 0 Then
                Dim testqry As New Query(Tables.Messages)
                testqry.AddWhere(Messages.Columns.Memberid, MemGuid)
                testqry.QueryType = QueryType.Select
                testqry.Top = "1"
                testqry.OrderBy = OrderBy.Asc(Messages.Columns.Id)
                testqry.SelectList = Messages.Columns.Postdate
                Dim rdr As IDataReader = testqry.ExecuteReader()
                Dim LastDate As DateTime
                While rdr.Read()
                    LastDate = rdr.GetDateTime(0)
                End While
                rdr.Close()
                _lastpostdate = LastDate.ToString("d")
            Else
                _lastpostdate = memUser.CreationDate()
            End If

            If String.IsNullOrEmpty(_displayname.Trim()) Then
                _displayname = _loginname
            End If
        End Sub

        Public Shared Sub Update(ByVal fuser As Member)
            Dim qry As New Query(Tables.MemberProfile)
            qry.QueryType = QueryType.Update
            qry.AddWhere(MemberProfile.Columns.Memberid, fuser.MemberId)
            qry.AddUpdateSetting(MemberProfile.Columns.Signature, fuser.Signature)
            qry.AddUpdateSetting(MemberProfile.Columns.Bio, fuser.Bio)
            qry.AddUpdateSetting(MemberProfile.Columns.Sortdesc, fuser.SortDescending)
            qry.AddUpdateSetting(MemberProfile.Columns.Msgsperpage, fuser.MsgsPerPage)
            qry.AddUpdateSetting(MemberProfile.Columns.Showavatars, fuser.ShowAvatars)
            qry.AddUpdateSetting(MemberProfile.Columns.Showsignatures, fuser.ShowSignatures)
            qry.AddUpdateSetting(MemberProfile.Columns.Sendwatchemails, fuser.SendWatchEmails)
            qry.AddUpdateSetting(MemberProfile.Columns.Showemail, fuser.ShowEmail)
            qry.Execute()
        End Sub

        Public Shared Function GetForumUser(ByVal memberid As Object) As Member
            Return New Member(memberid)
        End Function

        Public Shared Function GetCachedForumUser(ByVal memberid As Object) As Member
            Dim fUser As Member = CType(Web.HttpContext.Current.Cache.Get("fu" & memberid.ToString()), Member)
            If Not fUser Is Nothing Then Return fUser
            fUser = New Member(memberid)
            Web.HttpContext.Current.Cache.Add("fu" & memberid.ToString(), fUser, Nothing, DateAdd(DateInterval.Second, 90, Now), System.Web.Caching.Cache.NoSlidingExpiration, Web.Caching.CacheItemPriority.Normal, Nothing)
            Return fUser
        End Function

        Public Shared Function GetCurrentForumUser() As Member
            Return GetCachedForumUser(Web.Security.Membership.GetUser.ProviderUserKey)
        End Function

        Public Shared Sub ClearCache(ByVal memberid As Object)
            Web.HttpContext.Current.Cache.Remove("fu" & memberid.ToString())
        End Sub

#Region "Properties"
        Public Property MemberId() As Object
            Get
                Return _memberid
            End Get
            Set(ByVal value As Object)
                _memberid = value
            End Set
        End Property

        Public ReadOnly Property Joined() As Date
            Get
                Return _joined
            End Get
        End Property

        Public ReadOnly Property Online() As Boolean
            Get
                Return _online
            End Get
        End Property

        Public ReadOnly Property Email() As String
            Get
                Return _email
            End Get
        End Property

        Public ReadOnly Property LoginName() As String
            Get
                Return _loginname
            End Get
        End Property

        Public ReadOnly Property Posts() As Integer
            Get
                Return _posts
            End Get
        End Property

        Public ReadOnly Property DisplayName() As String
            Get
                Return _displayname
            End Get
        End Property

        Public ReadOnly Property Address() As String
            Get
                Return _address
            End Get
        End Property

        Public ReadOnly Property Phone() As String
            Get
                Return _phone
            End Get
        End Property

        Public ReadOnly Property LastPostDate() As Date
            Get
                Return _lastpostdate
            End Get
        End Property

        Public ReadOnly Property LastLoginDate() As Date
            Get
                Return _lastlogindate
            End Get
        End Property

        Public Property Signature() As String
            Get
                Return _signature
            End Get
            Set(ByVal value As String)
                _signature = value
            End Set
        End Property

        Public Property Bio() As String
            Get
                Return _bio
            End Get
            Set(ByVal value As String)
                _bio = value
            End Set
        End Property

        Public Property SortDescending() As Boolean
            Get
                Return _sortdesc
            End Get
            Set(ByVal value As Boolean)
                _sortdesc = value
            End Set
        End Property

        Public Property MsgsPerPage() As Integer
            Get
                Return _msgsperpage
            End Get
            Set(ByVal value As Integer)
                _msgsperpage = value
            End Set
        End Property

        Public Property ShowAvatars() As Boolean
            Get
                Return _showavatars
            End Get
            Set(ByVal value As Boolean)
                _showavatars = value
            End Set
        End Property

        Public Property ShowSignatures() As Boolean
            Get
                Return _showsignatures
            End Get
            Set(ByVal value As Boolean)
                _showsignatures = value
            End Set
        End Property

        Public Property SendWatchEmails() As Boolean
            Get
                Return _sendwatchemails
            End Get
            Set(ByVal value As Boolean)
                _sendwatchemails = value
            End Set
        End Property

        Public Property ShowEmail() As Boolean
            Get
                Return _showemail
            End Get
            Set(ByVal value As Boolean)
                _showemail = value
            End Set
        End Property

#End Region

        Public Shared Function FirstAndLastName(ByVal memkey As Object) As String
            Dim str As String = ""
            Dim Mem As New MemberInfo(MemberInfo.Columns.Memberid, memkey)
            If Mem.IsLoaded = True Then
                str = Mem.Firstname
                str += " "
                str += Mem.Lastname
            End If
            Return str
        End Function
    End Class
End Namespace